package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.*;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class TuiHuoService {
    @Autowired
    TuiHuoXiangQinMapper tuiHuoXiangQinMapper;
    @Autowired
    TuiHuoXiangQinDao tuiHuoXiangQinDao;
    @Autowired
    TuiHuoDao tuiHuoDao;
    @Autowired
    TuiHuoMapper tuiHuoMapper;

    @Autowired
    HuanHuoXiangQinDao huanHuoXiangQinDao;
    @Autowired
    HuanHuoDao huanHuoDao;
    @Autowired
    HuanHuoMapper huanHuoMapper;
    @Autowired
    HuanHuoXiangQinMapper huanHuoXiangQinMapper;


    @Autowired
    ShenHeDao shenHeDao;

    //查看所有退货详情
    public List<Tuihuoxiangqing> selectAllTuiHuoXiangQin(){
        return tuiHuoXiangQinMapper.selectAllTuiHuoXiangQin();
    }

    //查看所有换货详情
    public List<Huanhuoxiangqing> selectAllHuanHuoXiangQin(){
        return huanHuoXiangQinMapper.selectAllHuanHuoXiangQin();
    }

    //根据退货id查看订单对象
    public Dingdan selectDingdanByThId(Integer thId){
        return tuiHuoMapper.selectDingdanByThId(thId);
    }
    //根据换货id查看订单对象
    public Dingdan selectDingdanByhhId(Integer hhId){
        return huanHuoMapper.selectDingdanByhhId(hhId);
    }

    //新增退货
    public void addTuiHuo(Tuihuo tuihuo) {
        tuihuo.setThYs(1);
        Tuihuoxiangqing tx = null;

        if(tuihuo.getThId()!=0){
            tuiHuoXiangQinDao.deleteTuiHuoXiangQing(tuihuo.getThId());
            tuihuo.setThSh(tuihuo.getThSh());
            tuiHuoDao.save(tuihuo);
        }else if(tuihuo.getThId() == 0){
            if(tuihuo.getThWx().equals("是")){
               tuihuo.setThWxwc(-1);
            }
            tuihuo.setThSh("待审核");
            Tuihuo savev = tuiHuoDao.save(tuihuo);
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(savev.getThId());
            shenhe.setShShouming("退货");
            shenhe.setChuangJiantime(savev.getThTime());
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            shenhe.setShenqingPeople(savev.getThPeople());
            System.err.println("有包：" + shenhe.getShZhuangtai());
            shenHeDao.save(shenhe);
        }
        for(int i=0;i<tuihuo.fhxqList.size();i++){
            tx = new Tuihuoxiangqing();
            tx.setProductt(tuihuo.fhxqList.get(i).getProductt());
            tx.setThcpNum(tuihuo.fhxqList.get(i).getFhcpNum());
            tx.setThcpPrice(tuihuo.fhxqList.get(i).getFhcpPrice());
            tx.setThcpXiaoji(tuihuo.fhxqList.get(i).getFhcpXiaoji());
            tx.setTuihuoByThId(tuihuo);
            tuiHuoXiangQinDao.save(tx);
        }
    }

    //查询所有退货
    public List<Tuihuo> selectAllTuiHuo(String kName, String ddHao, String time,String empName) {
        return tuiHuoMapper.selectAllTuiHuo(kName,ddHao,time,empName);
    }

    //根据退货id查看退货详情
    public List<Tuihuoxiangqing> ckTuiHuoXiangQin(Integer thId) {
        return tuiHuoXiangQinMapper.ckTuiHuoXiangQin(thId);
    }

    //移除退货
    public String yichuTuiHuo(Integer thId){
        String fh = null;
        Tuihuo tuihuo = tuiHuoMapper.selectTuiHuoByThId(thId);
        System.out.println("sssS:"+tuihuo.getThSh());
        //如果退货单的审核状态为待审核，就可以进行物理删除
        //并且在审核表里面也要删除这条交付计划的审核
        if(tuihuo.getThSh().equals("待审核")){
            shenHeDao.delectShenHe(thId,"退货");
            tuiHuoXiangQinDao.deleteTuiHuoXiangQing(thId);
            tuiHuoDao.deleteTuiHuo(thId);
            fh = "删除成功";
        }else if(tuihuo.getThSh().equals("否决")){
            //审核状态为否决就可以移除
           // tuiHuoDao.yichuTuiHuo(thId);
            fh = "移除成功";
        }else if(tuihuo.getThSh().equals("同意")){
            fh = "移除失败，此退货单已通过审核";
        }
        return fh;
    }



    //根据选择的客户查询客户所有的退货单
    public List<Tuihuo> ckthd(Integer kId){
        return tuiHuoMapper.ckthd(kId);
    }


    //根据选择的客户查询客户所有的退货单（审核状态为同意，并且已经入库）
    public List<Tuihuo> tuiKuanCkthd(Integer kId){
        return tuiHuoMapper.tuiKuanCkthd(kId);
    }


    //换货=============================================
    //新增换货
    public void addHuanHuo(Huanhuo huanhuo) {
        huanhuo.setHhYs(1);
        Huanhuoxiangqing tx = null;

        System.err.println("换货id："+huanhuo.getHhId());
        if(huanhuo.getHhId()!=0){
            huanHuoXiangQinDao.deleteHuanHuoXiangQing(huanhuo.getHhId());
            huanhuo.setHhSh(huanhuo.getHhSh());
            huanHuoDao.save(huanhuo);
        }else if(huanhuo.getHhId() == 0){
            if(huanhuo.getHhWx().equals("是")){
                huanhuo.setHhWxwc(-1);
            }
            huanhuo.setHhSh("待审核");
            Huanhuo savev =  huanHuoDao.save(huanhuo);
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(savev.getHhId());
            shenhe.setShShouming("换货" );
            shenhe.setChuangJiantime(savev.getHhTime());
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            shenhe.setShenqingPeople(savev.getHhPeople());
            System.err.println("有包：" + shenhe.getShZhuangtai());
            shenHeDao.save(shenhe);
        }
        for(int i=0;i<huanhuo.hhfhList.size();i++){
            tx = new Huanhuoxiangqing();
            tx.setProductt(huanhuo.hhfhList.get(i).getProductt());
            tx.setHhcpNum(huanhuo.hhfhList.get(i).getFhcpNum());
            tx.setHhcpPrice(huanhuo.hhfhList.get(i).getFhcpPrice());
            tx.setHhcpXiaoji(huanhuo.hhfhList.get(i).getFhcpXiaoji());
            tx.setHuanhuoByHhId(huanhuo);
            huanHuoXiangQinDao.save(tx);
        }
    }

    //查看所有换货
    public List<Huanhuo> selectAllHuanHuo(String kName, String ddHao, String time,String empName) {
        return huanHuoMapper.selectAllHuanHuo(kName,ddHao,time,empName);
    }

    //根据换货id查找换货详情
    public List<Huanhuoxiangqing> ckHuanHuoXiangQin(Integer hhId) {
        return huanHuoXiangQinMapper.ckHuanHuoXiangQin(hhId);
    }

    //移除换货
    public String yichuHuanHuo(Integer hhId) {
        String fh = null;
        Huanhuo huanhuo = huanHuoMapper.selectHuanHuoByThId(hhId);
        //如果换货单的审核状态为待审核，就可以进行物理删除
        //并且在审核表里面也要删除这条交付计划的审核
        if(huanhuo.getHhSh().equals("待审核")){
            shenHeDao.delectShenHe(hhId,"换货");
            huanHuoXiangQinDao.deleteHuanHuoXiangQing(hhId);
            huanHuoDao.deleteHuanHuo(hhId);
            fh = "删除成功";
        }else if(huanhuo.getHhSh().equals("否决")){
            //审核状态为否决就可以移除
           // huanHuoDao.yichuHuanHuo(hhId);
            fh = "移除成功";
        }else if(huanhuo.getHhSh().equals("同意")){
            fh = "移除失败，此换货单已通过审核";
        }
        return fh;
    }


    public List<Huanhuo> fhckHuanHuoDan() {
        return huanHuoMapper.fhckHuanHuoDan();
    }



    //根据发货id查看退货详情
    public List<Tuihuoxiangqing> selectTHXQbyFhId(Integer fhId){
        return tuiHuoXiangQinMapper.selectTHXQbyFhId(fhId);
    }
    //根据发货id查看换货详情
    public List<Huanhuoxiangqing> selectHHXQbyFhId(Integer fhId){
        return huanHuoXiangQinMapper.selectHHXQbyFhId(fhId);
    }


    //查看所有审核同意并且选择维修的退货单
    public List<Tuihuo> selectwxTuiHuo(){
        return tuiHuoMapper.selectwxTuiHuo();
    }

    //查看所有审核同意并且选择维修的换货单
    @RequestMapping("/selectwxHuanHuo")
    @ResponseBody
    public List<Huanhuo> selectwxHuanHuo(){
        return huanHuoMapper.selectwxHuanHuo();
    }
}
